<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">奇幻旅程TRPG</div>
            <el-form :model="form" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="username" v-show="showL">
                    <el-input v-model="form.email" placeholder="email">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password" v-show="showL">
                    <el-input
                        type="password"
                        placeholder="password"
                        v-model="form.password"
                        @keyup.enter.native="submitForm()"
                    >
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="username" v-show="showR">
                    <el-input v-model="form.email" placeholder="email">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="username" v-show="showR">
                    <el-input v-model="form.phone" placeholder="phone">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="username" v-show="showR">
                    <el-input v-model="form.nickname" placeholder="nickname">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password" v-show="showR">
                    <el-input
                        type="password"
                        placeholder="password"
                        v-model="form.password"
                    >
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password" v-show="showR">
                    <el-input
                        type="password"
                        placeholder="password again"
                        v-model="form.repassword"
                    >
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="Login()" v-show="showL">登录</el-button>
                </div>
                <div class="login-btn">
                    <el-button type="primary" @click="toRegist()" v-show="showL">去注册</el-button>
                </div>
                <div class="login-btn">
                    <el-button type="primary" @click="Regist()" v-show="showR">注册</el-button>
                </div>
                <div class="login-btn">
                    <el-button type="primary" @click="toLogin()" v-show="showR">去登录</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
import axios from "axios";

export default {
    data: function() {
        return {
            showL: true,
            showR: false,
            form: {
                email: "",
                password: "",
                repassword: "",
                nickname: "",
                phone: ""
            },
            //表单的验证规则对象
            rules: {
                email: [
                    {required: true, message: '请输入用户账户（邮箱）', trigger: 'blur'},
                ],
                password: [
                    {required: true, message: '请输入密码', trigger: 'blur'},
                    {min: 3, max: 6, message: '长度在3到6个字符之间', trigger: 'blur'}
                ]
            }
        };
    },
    methods: {
        submitForm() {
            this.$refs.login.validate(valid => {
                if (valid) {
                    this.$message.success('登录成功');
                    localStorage.setItem('ms_username', this.param.username);
                    this.$router.push('/');
                } else {
                    this.$message.error('请输入账号和密码');
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        //登录
        async Login() {
            //表单预校验
            this.$refs.login.validate(async valid => {
                //通过预验证则valid=true
                if (!valid) {
                    this.$message.error("账号或密码格式错误");
                    return;
                }
                // const {data:res} = await axios.get(axios.defaults.baseURL+'login',this.form);
                axios({
                    methods: 'get',
                    url: axios.defaults.baseURL + 'login',
                    params: this.form
                }).then((res) => {
                    // this.$message.success("接收到了信息")
                    console.log(res)
                    if (res.data.code === '000000') {
                        this.$message.success("登陆成功")
                        localStorage.setItem("uid", res.data.content.uid);
                        localStorage.setItem("token", res.data.content.token);
                        localStorage.setItem("nickname", res.data.content.nickname);
                        this.$router.push('/');
                    }else{
                        this.$message.error(res.data.desc)
                    }
                })
                // .catch((error) => {
                //   console.log(error);
                // });
                // if(res.code==="000000"){
                //   this.$message.success("登陆成功");
                //   //设置cookie，时间为1hour
                //   this.$cookies.set("uid",res.content.uid,3600);
                //   this.$cookies.set("password",this.form.password,3600);
                //   this.$cookies.set("Token",res.content.Token,3600);
                //   sessionStorage.setItem("uid",res.content.uid);
                //   sessionStorage.setItem("token",res.content.Token);
                //   await this.$router.push('/index');
                // }else{
                //   this.$message.error(res.desc);
                // }
            })
        },
        //cookie登录
        async CLogin() {
            if (this.$cookies.get("uid") != null && this.$cookies.get("password") != null) {
                // this.form.pass=Base64.decode(this.$cookies.get("pass"));
                this.form.password = this.$cookies.get("password");
                this.form.email = this.$cookies.get("email");
                let {data: res} = await this.$http.post(axios.defaults.baseURL + 'login', this.form);
                if (res.code === "000000") {
                    this.$message.success("cookie登陆成功");
                    sessionStorage.setItem("uid", res.content.userId);
                    sessionStorage.setItem("token", res.content.token);
                    await this.$router.push('/index');
                } else {
                    this.$message.error(res.desc);
                }
            } else {
                this.$message.error("未找到cookie");
            }
        },
        //注册
        async Regist() {
            if (this.form.email.length === 0) {
                this.$message.error("邮箱不得为空")
                return;
            }
            this.$refs.login.validate(async valid => {
                if (!valid) {
                    this.$message.error("输入格式错误")
                    return;
                }
                if (this.form.password !== this.form.repassword) {
                    this.$message.error("两次输入密码不一致")
                    return;
                }

                const {data: res} = await this.$http.post(axios.defaults.baseURL + 'auth/register', this.form)
                if (res.code === "000000") {
                    this.$message.success("注册成功");
                    this.toLogin();
                } else {
                    this.$message.error(res.desc);
                }

            })
        },

        toRegist() {
            this.showL = false;
            this.showR = true;
            this.form.passAgain = "";
            this.form.pass = "";
            this.form.id = "";
        },
        toLogin() {
            this.showL = true;
            this.showR = false;
            this.form.passAgain = "";
            this.form.pass = "";
            this.form.id = "";
        }
    },
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: darkblue;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>